<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/art-template/template-web.js"></script>
  <script src="./libs/layui/lay/modules/layer.js"></script>
  <script src="./libs/layui/layui.js"></script>
  <script src="./js/http.js"></script>

  <script id="art_comment" type="text/html">
    {{each data}}
    {{if $value.state ==='已拒绝'}}
    <tr class="danger">
      {{else}}
      <tr>
    {{/if}}    
      <td>{{$value.author}}</td>
      <td>{{$value.content}}</td>
      <td>{{$value.title}}</td>
      <td>{{$value.date}}</td>
      <td>{{$value.state}}</td>
      <td class="text-center">
        {{if $value.state === '待审核'}}
          <a href="javascript:void(0);" onclick="register_click_event_for_btnAccept({{$value.id}})"  class="btn btn-success btn-xs btnAccept">批准</a>
          <a href="javascript:void(0);" onclick="register_click_event_for_btnReject({{$value.id}})"  class="btn btn-warning btn-xs btnReject">拒绝</a>
          {{else if $value.state === '已通过'}}
          <a href="javascript:void(0);" onclick="register_click_event_for_btnReject({{$value.id}})"  class="btn btn-warning btn-xs btnReject">拒绝</a>
          {{else}}
          <a href="javascript:void(0);" onclick="register_click_event_for_btnAccept({{$value.id}})" class="btn btn-success btn-xs btnAccept">批准</a>
        {{/if}}
        <a href="javascript:void(0);" onclick="register_click_event_for_btnDelete({{$value.id}})"  class="btn btn-danger btn-xs btnDelete">删除</a>
      </td>
    </tr>
    {{/each}}
  </script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th style="width:7%">作者</th>
            <th style="width:25%">评论</th>
            <th style="width:25%">评论在</th>
            <th style="width:15%">提交于</th>
            <th style="width:6%">状态</th>
            <th class="text-center" style="width:12%">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>小周</td>
            <td>这是一条测试评论，欢迎光临</td>
            <td>《世界，你好》</td>
            <td>2017-07-04 12:00:00</td>
            <td>已批准</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
              <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>
          <tr>
            <td>中周</td>
            <td>How are you?</td>
            <td>《世界，你好》</td>
            <td>2017-07-09 18:22:00</td>
            <td>待审核</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>

          <tr class="danger">
            <td>武秀英</td>
            <td>外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。</td>
            <td>《世界，你好》</td>
            <td>1970-03-15 11:31:50</td>
            <td>已拒绝</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>

        </tbody>
      </table>
      <div  id="pager"  class="row text-center">       
      

    </div>
  </div>  
  </div>
  <script>
    $(function () {
      // 调用分页获取评论方法，不传实参，使用默认参数值
      get_comment_list();     
    })

    // 分页获取评论数据，pageIndex参数默认值为1，pageSize默认值为6
    function get_comment_list(pageIndex = 1, pageSize = 6) {

      http('GET', '/admin/comment/search', { page: pageIndex, perpage: pageSize }, function (resData) {
        // 调用art-templte模板渲染数据
        let trString = template('art_comment', resData.data);
        $('.table tbody').html(trString);

        // 初始化分页控件
        init_pager(pageIndex,resData.data.totalCount,pageSize);
        
      })

    }

    // 初始化分页控件
    function init_pager(currPage,totalCount, pageSize) {
      layui.use('laypage', function () {
        var laypage = layui.laypage;

        laypage.render({
          elem: 'pager'
          , count: totalCount //数据总数，从服务端得到
          , limit: pageSize // 每页显示条数
          , curr:currPage  // 当前页码
          , jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：
            //console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            //console.log(obj.limit); //得到每页显示的条数

            //首次不执行
            if (!first) {
              //do something
              get_comment_list(obj.curr, obj.limit);
            }
          }
        });
      });
    }

    // 给 class="btnAccept" 的审批通过按钮注册点击事件
    function register_click_event_for_btnAccept(aid) {
      http('POST','/admin/comment/pass',{id:aid},function(resData){
          get_comment_list();
        })
    }

     // 给 class="btnReject" 的审批通过按钮注册点击事件
     function register_click_event_for_btnReject(aid) {
      http('POST','/admin/comment/reject',{id:aid},function(resData){
          get_comment_list();
        })
    }

     // 给 class="btnDelete" 的审批通过按钮注册点击事件
     function register_click_event_for_btnDelete(aid) {
      http('POST','/admin/comment/delete',{id:aid},function(resData){
          get_comment_list();
        })
    }
  </script>
</body>

</html>